package easy;

public class Solution_965 {
    private int rootVal = -1;

    public boolean isUnivalTree(TreeNode root) {
        return dps(root);
    }

    private boolean dps(TreeNode node) {
        if (node == null) {
            return true;
        }
        if (rootVal == -1) {
            rootVal = node.val;
        } else if (rootVal != node.val) {
            return false;
        }
        return dps(node.left) && dps(node.right);
    }

    public static void main(String[] args) {
        Solution_965 model = new Solution_965();
        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(2);
        root.left.right = new TreeNode(2);
        root.right = new TreeNode(2);
        System.out.println(model.isUnivalTree(root));
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
